package com.itheima.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.serivce.DishFlavorService;
import com.itheima.serivce.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Transactional
    @Override
    //      保存菜品的基本信息到菜品表dish
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品表  dishDto 继承过dish
        this.save(dishDto);

        //获取菜品口味 数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历 添加口味表 关联的菜品表id
        flavors.stream().map((iter) -> {
            iter.setDishId(dishDto.getId());
            return iter;
        }).collect(Collectors.toList());
        //保存口味表
        dishFlavorService.saveBatch(flavors);

    }

    //根据id查询所有菜品和口味
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据id查询当前 菜品表
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        //将菜品数据 复制到dishDto
        BeanUtils.copyProperties(dish, dishDto);

        //设置查询条件
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        //将查询到的口味表 添加到dishDto
        dishDto.setFlavors(list);
        //返还数据
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //保存更新 菜品表   dishDto 继承过dish
        this.updateById(dishDto);

        //设置查询条件  先删除当前口味表  便于之后的更新
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(wrapper);

        //获取 口味表数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历 添加口味表 关联的菜品表id
//        flavors.forEach(new Consumer<DishFlavor>() {
//            @Override
//            public void accept(DishFlavor dishFlavor) {
//                dishFlavor.setDishId(dishDto.getId());
//            }
//        });

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        //保存口味表数据
        dishFlavorService.saveBatch(flavors);

    }


    @Override
    public void delete(List<Long> ids) {
        this.removeByIds(ids);

        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(wrapper);


    }

}
